package com.xckj.cycling.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.xckj.cycling.utils.DensityUtil

class ElectProgressView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val stroke = DensityUtil.dip2px(4f)
    private val paint = Paint()
    private var color = Color.parseColor("#1ED6C2")

    init {
        paint.strokeWidth = stroke.toFloat()
        paint.style = Paint.Style.STROKE
        paint.strokeCap = Paint.Cap.ROUND

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (isInEditMode) {
            return
        }
        val radius = measuredWidth / 2f - stroke
        val arc = stroke.toFloat()
        canvas?.apply {
            paint.setColor(Color.parseColor("#E5E5E5"))
            drawArc(
                arc,
                arc,
                measuredWidth.toFloat() - arc,
                measuredHeight.toFloat() - arc,
                135f,
                270f,
                false,
                paint
            )
            paint.setColor(color)
            drawArc(
                arc,
                arc,
                measuredWidth.toFloat() - arc,
                measuredHeight.toFloat() - arc,
                135f,
                (progress * 270f).toFloat(),
                false,
                paint
            )
        }
    }

    var progress: Double = 0.5
        set(value) {
            if (field != value) {
                field = value
                invalidate()
            }
        }
}